import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import router from "./router/index.js";
import "element-plus/theme-chalk/el-message-box.css";
import "element-plus/theme-chalk/el-message.css";
// 如果您正在使用CDN引入，请删除下面一行。
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import elementpuls from 'element-plus'
import store from "./store/index.js"
import zhCn from 'element-plus/es/locale/lang/zh-cn';
import VueOfficeExcel from '@vue-office/excel'
import '@vue-office/excel/lib/index.css'
import VueOfficeDocx from '@vue-office/docx'
import '@vue-office/docx/lib/index.css'
import Vue3PdfApp from 'vue3-pdf-app'

const localData = localStorage.getItem('r_vuex')
if (localData) {
    store.commit('dynamicMenu', JSON.parse(localData).menu.routerList)
    store.state.menu.routerList.forEach(item => {
        router.addRoute("main", item)
    })
}
//添加路由守卫
router.beforeEach((to, from) => {
    const token = localStorage.getItem("token");
    // 定义公开路径列表（无需登录）
    const publicPaths = ["/login", "/register"];

    // 未登录且访问非公开页 → 跳转到登录页
    if (!token && !publicPaths.includes(to.path)) {
        return "/login";
    }

    // 已登录但访问公开页（如登录/注册页）→ 跳转到首页
    if (token && publicPaths.includes(to.path)) {
        return "/";
    }

    // 其他情况放行
    return true;
});

const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}
//路由挂载
app.use(router)
//状态管理挂载
app.use(store)

app.component('vue-office-excel', VueOfficeExcel) // 全局注册
app.component('vue-office-docx', VueOfficeDocx) // 全局注册
app.component('vue3-pdf-app', Vue3PdfApp)
app.use(elementpuls, { locale: zhCn })
app.mount('#app')
